package com.fenxiangle.yueding.feature.login.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.entity.bo.LoginPram;
import com.fenxiangle.yueding.feature.login.contract.LoginContract;
import com.fenxiangle.yueding.feature.login.dependencies.login.DaggerLoginComponent;
import com.fenxiangle.yueding.feature.login.dependencies.login.LoginPresenterModule;
import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * Created by Home on 2018/9/1.
 */

public class BindingPhoneActivity extends BaseActivity implements LoginContract.View {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.edt_binding_phone)
    EditText mEdtBindingPhone;
    @BindView(R.id.btn_get_code)
    TextView mBtnGetCode;
    @BindView(R.id.edt_binding_code)
    EditText mEdtBindingCode;
    private String mUserUid;

    @Inject
    LoginContract.Presenter mPresenter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_bindingphone;
    }

    @Override
    protected void initInjector() {
        DaggerLoginComponent.builder().loginPresenterModule(new LoginPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initData() {
        initToolBar(toolbar, "绑定手机号码", true, true);
        mUserUid = this.getIntent().getStringExtra("user_uid");
    }

    @OnClick({R.id.btn_get_code, R.id.btn_binding})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_get_code:
                mPresenter.sendCode(mEdtBindingPhone.getText().toString());
                break;
            case R.id.btn_binding:
                mPresenter.bindPhone(mEdtBindingPhone.getText().toString(), mEdtBindingCode.getText().toString(), mUserUid);
                break;
        }
    }



    @Override
    public void showBindReferee(UserInfoBo s) {
        Intent intent = new Intent(this, RecommendPeopleActivity.class);
        intent.putExtra("user_uid",s.getUserUid());
        startActivity(intent);
        finish();
    }

    @Override
    public void showLoginSuccess() {
        finish();
    }

    @Override
    public void showBindPhone(UserInfoBo s) {
        finish();
    }




    @Override
    public void showCodeSuccess(LoginPram s) {
        showMsg("获取验证码成功!");
        mBtnGetCode.setText(s.getCode());
        onGetCode();
    }

    public void onGetCode() {
        final int count = 60;//倒计时10秒
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(count + 1)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        return count - aLong;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())//ui线程中进行控件更新
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        mBtnGetCode.setEnabled(false);
                        mBtnGetCode.setTextColor(AM.res().getColor(R.color.text_secondary));
                    }
                }).subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Long num) {

                mBtnGetCode.setText(num + "s");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                //回复原来初始状态
                mBtnGetCode.setEnabled(true);
                mBtnGetCode.setText("发送验证码");
                mBtnGetCode.setTextColor(AM.res().getColor(R.color.text_secondary));
            }
        });

    }

    @Override
    public void showError(String error) {
        showMsg(error);
    }
}
